﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_INV_DIVERT_ORDER
// * CreateDate    : 2012/10/9 9:29:39
// * Creater       : CodeSmith
// * LastChangeDate: 2012/10/9 9:29:39
// * LastChanger   : 2012/10/9 9:29:39
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Mst;
using ESOL.Business.Prod;

namespace ESOL.Business.Inv
{
    /// <summary>
    /// InvDivertOrder
    /// </summary>
    public partial class InvDivertOrderBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objInvDivertOrderEntity">Entity</param>
        /// <returns>Entity</returns>
        public static InvDivertOrderEntity GetInvDivertOrder(InvDivertOrderEntity objInvDivertOrderEntity)
        {
            return DataAccess.SelectSingle<InvDivertOrderEntity>(objInvDivertOrderEntity);
        }

        public static DataTable GetByCondition(string condition, string orderBy, string rowCount)
        {
            string sql = CommonFunctions.GetSelect(rowCount) + @"A.*,B.WAREHOUSE_CODE,B.WAREHOUSE_NAME,C.WAREHOUSE_CODE,C.WAREHOUSE_NAME,D.NAME_CN AS NAME_CN_ORDER,E.NAME_CN AS NAME_CN_CREATED,F.NAME_CN AS NAME_CN_MODIFIED,G.NAME_CN AS NAME_CN_APPROVED,H.PRODUCT_CODE AS PRODUCT_CODE,I.LOCATION_CODE AS IN_LOCATION,I.WAREHOUSE_CODE AS IN_WAREHOUSE,J.LOCATION_CODE AS OUT_LOCATION,J.WAREHOUSE_CODE AS OUT_WAREHOUSE from INV_DIVERT_ORDER A
                            left join MST_WAREHOUSE B on A.OUT_WAREHOUSE_CODE=B.WAREHOUSE_CODE
                            left join MST_WAREHOUSE C on A.IN_WAREHOUSE_CODE=C.WAREHOUSE_CODE
                            left join MST_PERSON D on A.ORDER_PERSON=D.PERSON_ID
                            left join MST_PERSON E on A.CREATED_BY=E.PERSON_ID
                            left join MST_PERSON F on A.LAST_MODIFIED_BY=F.PERSON_ID
                            left join MST_PERSON G on A.APPROVED_BY=G.PERSON_ID
                            left join MST_PRODUCT H on A.PRODUCT_ID=H.PRODUCT_ID
                            left join MST_LOCATION I on A.IN_LOCATION_ID=I.LOCATION_ID
                            left join MST_LOCATION J on A.OUT_LOCATION_ID=J.LOCATION_ID
                            WHERE 1=1";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;

        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objInvDivertOrderEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertInvDivertOrder(InvDivertOrderEntity objInvDivertOrderEntity)
        {
            return DataAccess.Insert(objInvDivertOrderEntity);
        }

        public static bool InsertInvDivertOrder(List<InvDivertOrderEntity> objInvDivertOrderEntityList)
        {
            return DataAccess.Insert(objInvDivertOrderEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objInvDivertOrderEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateInvDivertOrder(InvDivertOrderEntity objInvDivertOrderEntity)
        {
            return DataAccess.Update(objInvDivertOrderEntity);
        }

        public static bool UpdateInvDivertOrder(List<InvDivertOrderEntity> objInvDivertOrderEntityList)
        {
            return DataAccess.Update<InvDivertOrderEntity>(objInvDivertOrderEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objInvDivertOrderEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteInvDivertOrder(InvDivertOrderEntity objInvDivertOrderEntity)
        {
            return DataAccess.Delete(objInvDivertOrderEntity);
        }

        public static bool DeleteInvDivertOrder(List<InvDivertOrderEntity> objInvDivertOrderEntityList)
        {
            return DataAccess.Delete<InvDivertOrderEntity>(objInvDivertOrderEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetInvDivertOrderAll()
        {
            return DataAccess.SelectAllDataSet<InvDivertOrderEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<InvDivertOrderEntity> GetInvDivertOrderAllList()
        {
            return DataAccess.SelectAll<InvDivertOrderEntity>();
        }
        #endregion

        public static bool SaveDivertOrder(InvDivertOrderEntity entity,bool action)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (entity == null)
                    {
                        broker.RollBack();
                        return false;
                    }
                    if (action == true)//新增
                    {
                        string code = "";
                        if (MstCodeRuleBs.GetCurrentOrderCode("InvDivertOrder", out code, broker) == false)
                        {
                            broker.RollBack();
                            throw new Exception("自动生成出库申请单号失败！");
                        }
                        entity.DivertOrderId = Guid.NewGuid().ToString();
                        entity.OrderCode = code;

                        DataAccess.Insert(entity, broker);
                    }
                    else//修改
                    {
                        DataAccess.Update(entity, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch(Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

//        public static bool SaveApproveDivertOrder(InvDivertOrderEntity entity)
//        {
//            using (DataAccessBroker broker = DataAccessFactory.Instance())
//            {
//                try
//                {
//                    broker.BeginTransaction();

//                    entity.Status = "ICO03";
//                    DataAccess.Update(entity, broker);

//                    #region 出库
//                    InvStorageEntity entityStroage = new InvStorageEntity();
//                    InvStorageChangeEntity entityStroageChange = new InvStorageChangeEntity();
//                    ProdMrpQtyEntity entityMRP = new ProdMrpQtyEntity();


//                    //更新 Storage 表
//                    string sql = "select * from dbo.INV_STORAGE where PRODUCT_ID='" + entity.ProductId + "'";
//                    DataSet ds = broker.FillSQLDataSet(sql);
//                    if (ds.Tables[0].Rows.Count > 0)
//                    {

//                        string sql_inv = @"select A.*,B.LOCATION_CODE,C.WAREHOUSE_CODE,C.WAREHOUSE_NAME 
//                                           from VIEW_INV_STORAGE A 
//                                           left join MST_LOCATION B on A.LOCATION_ID=B.LOCATION_ID
//                                           left join MST_WAREHOUSE C on B.WAREHOUSE_CODE=C.WAREHOUSE_CODE
//                                           where A.PRODUCT_ID='" + entity.ProductId + "'";
//                        DataSet ds_inv = broker.FillSQLDataSet(sql_inv);
//                        if (ds_inv.Tables[0].Rows.Count > 0)
//                        {
//                            entityStroage.LocationId = ds_inv.Tables[0].Rows[0]["LOCATION_ID"].ToString();
//                            entityStroage.ProductId = ds_inv.Tables[0].Rows[0]["PRODUCT_ID"].ToString();
//                            entityStroage.FixedQty = Convert.ToDecimal(ds_inv.Tables[0].Rows[0]["FIXED_QTY"].ToString());
//                            entityStroage.CurrentQty = Convert.ToDecimal(ds_inv.Tables[0].Rows[0]["CURRENT_QTY"].ToString()) - entity.Qty;
//                            if (entityStroage.CurrentQty < 0)
//                            {
//                                throw new Exception("库存数量不足，不可以负出库！");
//                            }
//                            entityStroage.LastRelatedOrder = entity.DivertOrderId;
//                            entityStroage.LastUpdateDate = DateTime.Now;
//                            entityStroage.LastModifiedBy = CommonParameter.CurrentUser.PersonId;//InvDispatchOrderBs.GetInvDispatchOrderByID(entity.DispatchOrderId).ApprovedBy;
//                            DataAccess.Update(entityStroage, broker);
//                        }
//                        else
//                        {
//                            broker.RollBack();
//                            return false;
//                        }

//                        //更新 StorageChange 表
//                        entityStroageChange.ChangeId = Guid.NewGuid().ToString();
//                        entityStroageChange.ChangeQty = entity.Qty;
//                        entityStroageChange.ChangeTime = DateTime.Now;
//                        entityStroageChange.ChangeUser = CommonParameter.CurrentUser.PersonId;//InvDispatchOrderBs.GetInvDispatchOrderByID(entity.DispatchOrderId).ApprovedBy;
//                        entityStroageChange.ProductId = entity.ProductId;
//                        entityStroageChange.RelatedOrder = entity.DivertOrderId;
//                        entityStroageChange.LocationId = entityStroage.LocationId;
//                        entityStroageChange.Remark = entity.Remark;
//                        entityStroageChange.ChangeType = "INVC01";
//                        DataAccess.Insert(entityStroageChange, broker);

//                        //更新可用量表

//                        string Sql_Mrp = "select * from PROD_MRP_QTY where PRODUCT_ID='" + entity.ProductId + "'";
//                        DataSet ds_Mrp = broker.FillSQLDataSet(Sql_Mrp);
//                        if (ds_Mrp.Tables[0].Rows.Count > 0)
//                        {
//                            entityMRP.ProductId = entity.ProductId;
//                            entityMRP.CurrentStorageQty = entityMRP.CurrentStorageQty - entity.Qty;
//                            //entityMRP.WareHouseCode = dr["WAREHOUSE_CODE"].ToString();
//                            DataAccess.Update(entityMRP, broker);
//                        }
//                        else
//                        {
//                            entityMRP.ProductId = entity.ProductId;
//                            entityMRP.CurrentStorageQty = entityMRP.CurrentStorageQty - entity.Qty;
//                            //entityMRP.WareHouseCode = dr["WAREHOUSE_CODE"].ToString();
//                            entityMRP.PlanPurchaseQty = 0;
//                            entityMRP.AvailableQty = 0;
//                            DataAccess.Insert(entityMRP, broker);
//                        }
//                    #endregion

//                    #region 入库
//                        //更新库存表
//                    InvStorageEntity entityStroage2 = new InvStorageEntity();
//                    InvStorageChangeEntity entityStroageChange2 = new InvStorageChangeEntity();
//                    ProdMrpQtyEntity entityMRP2 = new ProdMrpQtyEntity();

//                    string sql2 = "select * from dbo.INV_STORAGE where PRODUCT_ID='" + entity.ProductId + "' and LOCATION_ID='"+entity.InLocationId+"'";
//                    DataSet ds2 = broker.FillSQLDataSet(sql2);

//                    if (ds2.Tables[0].Rows.Count > 0)
//                    {

//                        //更新 Storage 表
//                        entityStroage2.ProductId = entity.ProductId;
//                        entityStroage2.LocationId = entity.InLocationId;
//                        entityStroage2.CurrentQty = Convert.ToDecimal(ds2.Tables[0].Rows[0]["CURRENT_QTY"].ToString())+ entity.Qty;
//                        entityStroage2.FixedQty = Convert.ToDecimal(ds2.Tables[0].Rows[0]["FIXED_QTY"].ToString());
//                        entityStroage2.LastRelatedOrder = entity.DivertOrderId;
//                        entityStroage2.LastUpdateDate = DateTime.Now;
//                        entityStroage2.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;//InvImportOrderBs.GetInvImportOrderByID(entity.ImportOrderId).ApprovedBy;
//                        DataAccess.Update(entityStroage2, broker);

//                    }
//                    else
//                    {

//                        //更新 Storage 表
//                        entityStroage2.ProductId = entity.ProductId;
//                        entityStroage2.LocationId = entity.InLocationId;
//                        entityStroage2.CurrentQty = entity.Qty;
//                        entityStroage2.FixedQty = 0;
//                        entityStroage2.LastRelatedOrder = entity.DivertOrderId;
//                        entityStroage2.LastUpdateDate = DateTime.Now;
//                        entityStroage2.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;//InvImportOrderBs.GetInvImportOrderByID(entity.ImportOrderId).ApprovedBy;
//                        DataAccess.Insert(entityStroage2, broker);
//                    }
//                        //更新 StorageChange 表
//                        entityStroageChange2.ChangeId = Guid.NewGuid().ToString();
//                        entityStroageChange2.ChangeQty = entity.Qty;
//                        entityStroageChange2.ChangeTime = DateTime.Now;
//                        entityStroageChange2.ChangeUser = Common.CommonParameter.CurrentUser.PersonId;//InvImportOrderBs.GetInvImportOrderByID(entity.ImportOrderId).ApprovedBy;
//                        entityStroageChange2.ProductId = entity.ProductId;
//                        entityStroageChange2.RelatedOrder = entity.DivertOrderId;
//                        entityStroageChange2.LocationId = entityStroage.LocationId;
//                        entityStroageChange2.Remark = entity.Remark;
//                        entityStroageChange2.ChangeType = "INVC02";
//                        DataAccess.Insert(entityStroageChange2, broker);


//                        //更新可用量表
//                        if (ProdMrpQtyBs.GetByProductId(entity.ProductId) != null)
//                        {
//                            entityMRP2.ProductId = entity.ProductId;
//                            entityMRP2.CurrentStorageQty = entityMRP.CurrentStorageQty + entity.Qty;
//                            //entityMRP.WareHouseCode = dr["WAREHOUSE_CODE"].ToString();
//                            DataAccess.Update(entityMRP2, broker);
//                        }
//                        else
//                        {
//                            entityMRP2.ProductId = entity.ProductId;
//                            entityMRP2.CurrentStorageQty = entityMRP2.CurrentStorageQty + entity.Qty;
//                            //entityMRP.WareHouseCode = dr["WAREHOUSE_CODE"].ToString();
//                            entityMRP2.PlanPurchaseQty = 0;
//                            entityMRP2.AvailableQty = 0;
//                            DataAccess.Insert(entityMRP2, broker);
//                        }

//                    }
//                    #endregion

//                    broker.Commit();
//                    return true;
//                }
//                catch (Exception ex)
//                {
//                    broker.RollBack();
//                    throw ex;
//                }
//            }
//        }

        public static bool SaveApproveDivertOrder2(InvDivertOrderEntity entity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();

                    entity.Status = "ICO03";
                    DataAccess.Update(entity, broker);

                    InvImportOrderEntity EntityImp = new InvImportOrderEntity();
                    InvDispatchOrderEntity EntityDis = new InvDispatchOrderEntity();

                    //入库主单
                    string codeImp = "";
                    if (MstCodeRuleBs.GetCurrentOrderCode("InvImportOrder", out codeImp) == false)
                    {
                        broker.RollBack();
                        return false;
                        throw new Exception("自动生成入库申请单号失败！");
                    }
                    EntityImp.OrderCode = codeImp;
                    EntityImp.ImportOrderId = Guid.NewGuid().ToString();
                    EntityImp.OrderType = "IMT06";
                    EntityImp.Status = "IMO03";
                    EntityImp.OrderDate = entity.OrderDate.ToString();
                    EntityImp.OrderPerson = entity.OrderPerson;
                    EntityImp.Remark = "移库时自动产生的入库单";
                    EntityImp.CreatedBy = entity.CreatedBy;
                    EntityImp.CreatedDate = entity.CreatedDate;
                    EntityImp.LastModifiedBy = entity.LastModifiedBy;
                    EntityImp.LastModifiedDate = entity.LastModifiedDate;
                    EntityImp.ApprovedBy = Common.CommonParameter.CurrentUser.PersonId;
                    EntityImp.ApprovedDate = DateTime.Now;

                    //出库主单
                    string codeDis = "";
                    if (MstCodeRuleBs.GetCurrentOrderCode("InvDispatchOrder", out codeDis) == false)
                    {
                        broker.RollBack();
                        return false;
                        throw new Exception("自动生成出库申请单号失败！");
                    }
                    EntityDis.OrderCode = codeDis;
                    EntityDis.DispatchOrderId = Guid.NewGuid().ToString();
                    EntityDis.OrderType = "IDT06";
                    EntityDis.Status = "IDO03";
                    EntityDis.OrderDate = entity.OrderDate;
                    EntityDis.OrderPerson = entity.OrderPerson;
                    EntityDis.Remark = "移库时自动产生的出库单";
                    EntityDis.CreatedBy = entity.CreatedBy;
                    EntityDis.CreatedDate = entity.CreatedDate;
                    EntityDis.LastModifiedBy = entity.LastModifiedBy;
                    EntityDis.LastModifiedDate = entity.LastModifiedDate;
                    EntityDis.ApprovedBy = Common.CommonParameter.CurrentUser.PersonId;
                    EntityDis.ApprovedDate = DateTime.Now;

                    //出库单明细
                    InvDispatchItemEntity DisItemEntity = new InvDispatchItemEntity();
                    DisItemEntity.DispatchItemId = Guid.NewGuid().ToString();
                    DisItemEntity.DispatchOrderId = EntityDis.DispatchOrderId;
                    DisItemEntity.ProductId = entity.ProductId;
                    DisItemEntity.Qty = entity.Qty;
                    DisItemEntity.Remark = "移库时自动产生";
                    DisItemEntity.LocationId = entity.OutLocationId;

                    //入库单明细
                    InvImportItemEntity ImpItemEntity = new InvImportItemEntity();
                    ImpItemEntity.ImportItemId = Guid.NewGuid().ToString();
                    ImpItemEntity.ImportOrderId = EntityImp.ImportOrderId;
                    ImpItemEntity.ProductId = entity.ProductId;
                    ImpItemEntity.Qty = entity.Qty;
                    ImpItemEntity.Remark = "移库时自动产生";
                    ImpItemEntity.LocationId = entity.InLocationId;

                    DataAccess.Insert(EntityDis, broker);
                    DataAccess.Insert(EntityImp, broker);
                    DataAccess.Insert(DisItemEntity, broker);
                    DataAccess.Insert(ImpItemEntity, broker);

                    //更新 Storage 表
                    string sql = "select * from dbo.INV_STORAGE where PRODUCT_ID='" + entity.ProductId + "' and LOCATION_ID='"+entity.OutLocationId+"'";
                    DataSet ds = broker.FillSQLDataSet(sql);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        #region 出库
                        InvDispatchLocationEntity entityLocation = new InvDispatchLocationEntity();
                        InvStorageEntity entityStroage = new InvStorageEntity();
                        InvStorageChangeEntity entityStroageChange = new InvStorageChangeEntity();
                        ProdMrpQtyEntity entityMRP = new ProdMrpQtyEntity();


                        string sql_inv = @"select A.*,B.LOCATION_CODE,C.WAREHOUSE_CODE,C.WAREHOUSE_NAME 
                                           from VIEW_INV_STORAGE A 
                                           left join MST_LOCATION B on A.LOCATION_ID=B.LOCATION_ID
                                           left join MST_WAREHOUSE C on B.WAREHOUSE_CODE=C.WAREHOUSE_CODE
                                           where A.PRODUCT_ID='" + entity.ProductId + "' AND A.LOCATION_ID='"+entity.OutLocationId+"'";
                        DataSet ds_inv = broker.FillSQLDataSet(sql_inv);
                        if (ds_inv.Tables[0].Rows.Count > 0)
                        {
                            entityStroage.LocationId = ds_inv.Tables[0].Rows[0]["LOCATION_ID"].ToString();
                            entityStroage.ProductId = ds_inv.Tables[0].Rows[0]["PRODUCT_ID"].ToString();
                            entityStroage.FixedQty = Convert.ToDecimal(ds_inv.Tables[0].Rows[0]["FIXED_QTY"].ToString());
                            entityStroage.CurrentQty = Convert.ToDecimal(ds_inv.Tables[0].Rows[0]["CURRENT_QTY"].ToString()) - entity.Qty;
                            if (entityStroage.CurrentQty < 0)
                            {
                                throw new Exception("库存数量不足，不可以负出库！");
                            }
                            entityStroage.LastRelatedOrder = entity.DivertOrderId;
                            entityStroage.LastUpdateDate = DateTime.Now;
                            entityStroage.LastModifiedBy = CommonParameter.CurrentUser.PersonId;//InvDispatchOrderBs.GetInvDispatchOrderByID(entity.DispatchOrderId).ApprovedBy;
                            DataAccess.Update(entityStroage, broker);
                        }
                        else
                        {
                            broker.RollBack();
                            return false;
                        }
                        //更新 ImportLocation 表

                        entityLocation.LocationId = entity.OutLocationId;
                        entityLocation.Qty = entity.Qty;
                        entityLocation.DispatchItemId = DisItemEntity.DispatchItemId;
                        DataAccess.Insert(entityLocation, broker);

                        //更新 StorageChange 表
                        entityStroageChange.ChangeId = Guid.NewGuid().ToString();
                        entityStroageChange.ChangeQty = entity.Qty;
                        entityStroageChange.ChangeTime = DateTime.Now;
                        entityStroageChange.ChangeUser = CommonParameter.CurrentUser.PersonId;//InvDispatchOrderBs.GetInvDispatchOrderByID(entity.DispatchOrderId).ApprovedBy;
                        entityStroageChange.ProductId = entity.ProductId;
                        entityStroageChange.RelatedOrder = DisItemEntity.DispatchItemId;
                        entityStroageChange.LocationId = entityStroage.LocationId;
                        entityStroageChange.Remark = entity.Remark;
                        entityStroageChange.ChangeType = "INVC01";
                        DataAccess.Insert(entityStroageChange, broker);

                        
                    #endregion

                        #region 入库
                        //更新库存表
                        InvImportLocationEntity entityLocation2 = new InvImportLocationEntity();
                        InvStorageEntity entityStroage2 = new InvStorageEntity();
                        InvStorageChangeEntity entityStroageChange2 = new InvStorageChangeEntity();
                        ProdMrpQtyEntity entityMRP2 = new ProdMrpQtyEntity();

                        string sql2 = "select * from dbo.INV_STORAGE where PRODUCT_ID='" + entity.ProductId + "' and LOCATION_ID='" + entity.InLocationId + "'";
                        DataSet ds2 = broker.FillSQLDataSet(sql2);

                        if (ds2.Tables[0].Rows.Count > 0)
                        {
                            //更新 Storage 表
                            entityStroage2.ProductId = entity.ProductId;
                            entityStroage2.LocationId = entity.InLocationId;
                            entityStroage2.CurrentQty = Convert.ToDecimal(ds2.Tables[0].Rows[0]["CURRENT_QTY"].ToString()) + entity.Qty;
                            entityStroage2.FixedQty = Convert.ToDecimal(ds2.Tables[0].Rows[0]["FIXED_QTY"].ToString());
                            entityStroage2.LastRelatedOrder = entity.DivertOrderId;
                            entityStroage2.LastUpdateDate = DateTime.Now;
                            entityStroage2.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;//InvImportOrderBs.GetInvImportOrderByID(entity.ImportOrderId).ApprovedBy;
                            DataAccess.Update(entityStroage2, broker);

                        }
                        else
                        {
                            //更新 Storage 表
                            entityStroage2.ProductId = entity.ProductId;
                            entityStroage2.LocationId = entity.InLocationId;
                            entityStroage2.CurrentQty = entity.Qty;
                            entityStroage2.FixedQty = 0;
                            entityStroage2.LastRelatedOrder = entity.DivertOrderId;
                            entityStroage2.LastUpdateDate = DateTime.Now;
                            entityStroage2.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;//InvImportOrderBs.GetInvImportOrderByID(entity.ImportOrderId).ApprovedBy;
                            DataAccess.Insert(entityStroage2, broker);
                        }
                        //更新 ImportLocation 表

                        entityLocation2.LocationId = entity.InLocationId;
                        entityLocation2.Qty = entity.Qty;
                        entityLocation2.ImportItemId = ImpItemEntity.ImportItemId;
                        DataAccess.Insert(entityLocation2, broker);


                        //更新 StorageChange 表
                        entityStroageChange2.ChangeId = Guid.NewGuid().ToString();
                        entityStroageChange2.ChangeQty = entity.Qty;
                        entityStroageChange2.ChangeTime = DateTime.Now;
                        entityStroageChange2.ChangeUser = Common.CommonParameter.CurrentUser.PersonId;//InvImportOrderBs.GetInvImportOrderByID(entity.ImportOrderId).ApprovedBy;
                        entityStroageChange2.ProductId = entity.ProductId;
                        entityStroageChange2.RelatedOrder = ImpItemEntity.ImportItemId;
                        entityStroageChange2.LocationId = entityStroage.LocationId;
                        entityStroageChange2.Remark = entity.Remark;
                        entityStroageChange2.ChangeType = "INVC02";
                        DataAccess.Insert(entityStroageChange2, broker);

                    #endregion
                    }

                    broker.Commit();
                    return true;

                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
    }


}

